# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

# 如果想要使用管道的话，就需要在setting中开启管道
class ScrapyDangdangTestPipeline:
    #在爬虫文件开始执行之前执行的方法
    def open_spider(self,spider):
        self.fp = open('book.json','w')
    # item 由yield传递过来
    def process_item(self, item, spider):


        with open('book.json','a',encoding='utf-8')as fp:
            fp.write(str(item))
        return item
    # 在爬虫文件结束之后执行
    def close_spider(self, spider):
        self.fp.close()
import urllib.request
class ScrapyDangdangDownLoadPipeline:
    def process_item(self, item, spider):
        url = 'http:' + item.get('src')
        filename = './books/' + item.get('name')+'.jpg'
        urllib.request.urlretrieve(url=url,filename=filename)
        return item
